﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weikebuluo.Model;
using weikebuluo.DAL;

namespace weikeweixin.web.admin.website
{
    public partial class quickadd : weikeweixin.web.admin.cs.adminbase
    {
        t_menu_dal ShortcutMenuDAL = new t_menu_dal();
        protected string BgColor = "";
        protected string Name = "";
        protected string PicUrl = "icon-bullhorn";
        protected string Sort = "";
        protected string Type = "link";
        protected string LinkUrl = "";
        protected string Status = "";
        protected string title = "添加快捷";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(user_id))
            {
                Response.Write("<script Language=\"Javascript\">alert(\"请先配置微官网\")");
                Response.Write("</script>");
                Response.End();
            }
            if (Request.QueryString["id"] != null)
            {
                title = "编辑快捷";
                System.Data.DataTable dt = ShortcutMenuDAL.GetList("Id=" + Request.QueryString["id"].Trim()).Tables[0];
                BgColor = dt.Rows[0]["BgColor"].ToString().Trim();
                Name = dt.Rows[0]["Name"].ToString().Trim();
                PicUrl = dt.Rows[0]["PicUrl"].ToString().Trim();
                Sort = dt.Rows[0]["Sort"].ToString().Trim();
                Type = dt.Rows[0]["Type2"].ToString().Trim();
                LinkUrl = dt.Rows[0]["LinkUrl"].ToString().Trim();
                Status = dt.Rows[0]["Status"].ToString().Trim();
            }
            switch (Request.Form["action"])
            {
                case "save":
                    Save();
                    break;
            }
        }

        private void Save()
        {
            t_menu_model ShortcutMenuModel = new t_menu_model();
            ShortcutMenuModel.Name = Request.Form["name"];
            ShortcutMenuModel.BgColor = Request.Form["BgColor"];
            ShortcutMenuModel.user_id = int.Parse(user_id);
            ShortcutMenuModel.PicUrl = Request.Form["icon"];
            ShortcutMenuModel.Sort = Request.Form["order"];
            ShortcutMenuModel.Type = "Menu";
            ShortcutMenuModel.Type2 = Request.Form["type"];
            ShortcutMenuModel.Status = int.Parse(string.IsNullOrEmpty(Request.Form["display"]) ? "1" : Request.Form["display"]);
            switch (ShortcutMenuModel.Type2)
            {
                case "link":
                    ShortcutMenuModel.LinkUrl = Request.Form["link"];
                    break;
                case "tel":
                    ShortcutMenuModel.LinkUrl = Request.Form["tel"];
                    break;
            }
            if (string.IsNullOrEmpty(Request.Form["id"]))
            {
                int re = ShortcutMenuDAL.Add(ShortcutMenuModel);
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                    AddButton();
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
            }
            else
            {
                ShortcutMenuModel.Id = int.Parse(Request.Form["id"]);
                bool re = ShortcutMenuDAL.Update(ShortcutMenuModel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"quick.aspx\",\"error\":\"\"}");
                }
            }
            Response.End();
        }

        public void AddButton()
        {
            int Count = ShortcutMenuDAL.GetRecordCount("user_id=" + user_id + " and Type='Button'");
            if (Count == 0)
            {
                t_menu_model ShortcutMenuModel = new t_menu_model();
                ShortcutMenuModel.BgColor = "red";
                ShortcutMenuModel.Status = 1;
                ShortcutMenuModel.Type = "Button";
                ShortcutMenuModel.Type2 = "Button"; 
                ShortcutMenuModel.user_id = int.Parse(user_id);
                ShortcutMenuDAL.Add(ShortcutMenuModel);
            }
        }
    }
}